package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.Report;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Reset password page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class ResetPasswordPage extends LoadableComponent<ResetPasswordPage> implements IPage {

    // ************************ Locators ************************

    private static final String RESET_PSWD_PAGE_URL = Properties.get("env.root.url") + "reset-password";

    private static final String EMAIL_FIELD = "reset_password_request_email";

    private static final String RESET_BTN = "//input[@value='Reset Password']";

    private static final String CANCEL_LINK = "//a[text()='Cancel']";

    private static final String LOGO = "oslogo";

    private static final String COPYRIGHT = "//p[@class='copyright' and contains(., '2011 The OpenSky Project, Inc.')]";

    private static final String BOTTOM_NAV = "//div[@class='footer-body']/ul[@class='links']";

    private static final String WRONG_EMAIL_ALERT = "//form[@action='/reset-password']//label[@for='reset_password_request_email']/span[contains(., \"Sorry, we couldn't find anyone with that email\")]";

    private static final String EMAIL_SEND_OK_ALERT = "//section/h1[contains(., \"We've sent you an email with a secure link. Click it to create or reset your password and you'll be good to go.\")]";

    private static final String SEND_IT_AGAIN_LINK = "//a[text()='send it again']";

    public final String NOT_REGISTERED_EMAIL = "not.registered@gmail.com";

    private final WebDriverWrapper driver;

    public ResetPasswordPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(Report.wrongURL(RESET_PSWD_PAGE_URL, url), LogicUtils.isURLEqual(RESET_PSWD_PAGE_URL, url));
    }

    public void resetPassword(String email) {
	driver.waitAndSendKeys("EMAIL_FIELD", By.id(EMAIL_FIELD), email);
	driver.waitAndClick("RESET_BTN", By.xpath(RESET_BTN));
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(RESET_PSWD_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(RESET_PSWD_PAGE_URL);
    }

    public boolean isBaseResetElementsPresent() {
	driver.waitAndVerifyElementPresent("EMAIL_FIELD", By.id(EMAIL_FIELD));
	driver.waitAndVerifyElementPresent("RESET_BTN", By.xpath(RESET_BTN));
	driver.waitAndVerifyElementPresent("CANCEL_LINK", By.xpath(CANCEL_LINK));

	return true;
    }

    public boolean isLogoAndCopyrightPresent() {
	driver.waitAndVerifyElementPresent("LOGO", By.id(LOGO));
	driver.waitAndVerifyElementPresent("COPYRIGHT", By.xpath(COPYRIGHT));

	return true;
    }

    public boolean isBottomNavPresent() {
	driver.waitAndVerifyElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV));

	return true;
    }

    public boolean isWrongEmailAlertPresent() {
	driver.waitAndVerifyElementPresent("WRONG_EMAIL_ALERT", By.xpath(WRONG_EMAIL_ALERT));

	return true;
    }

    public boolean isEmailSendOkAlertAndSendAgainPresent() {
	driver.waitAndVerifyElementPresent("EMAIL_SEND_OK_ALERT", By.xpath(EMAIL_SEND_OK_ALERT));
	driver.waitAndVerifyElementPresent("SEND_IT_AGAIN_LINK", By.xpath(SEND_IT_AGAIN_LINK));

	return true;
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Reset password page";
    }
}
